package com.soft.servlet;

import com.soft.dao.CommodityDAO;
import com.soft.entity.Commodity;
import com.soft.util.PageBean;

import java.io.IOException;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//产品管理,图片上传
@WebServlet({"/CommodityServlet"})
public class CommodityServlet
        extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private String uri;

    public void destroy() {
        super.destroy();
    }


    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");


        String method = request.getParameter("method");


        if ("list".equals(method)) {

            list(request, response);

        } else if ("search".equals(method)) {

            search(request, response);

        } else if ("delete".equals(method)) {

            delete(request, response);

        } else if ("Jumpup".equals(method)) {

            Jumpup(request, response);
        }
    }


    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }


    public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            String currPage = request.getParameter("currentPage");

            if (currPage == null || "".equals(currPage.trim())) {
                currPage = "1";
            }

            int currentPage = Integer.parseInt(currPage);


            PageBean pageBean = new PageBean();
            pageBean.setCurrentPage(currentPage);

            CommodityDAO dao = new CommodityDAO();

            Map<String, Object> result = dao.getAll(pageBean);

            request.setAttribute("result", result);

            this.uri = "/sys/goods/goods.jsp";
        } catch (Exception e) {
            e.printStackTrace();
        }


        request.getRequestDispatcher(this.uri).forward((ServletRequest) request, (ServletResponse) response);
    }


    public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            int CommodityId = Integer.parseInt(request.getParameter("id"));

            Commodity cc = new Commodity();
            cc.setId(CommodityId);


            CommodityDAO dao = new CommodityDAO();
            dao.delete(cc);


            this.uri = "/CommodityServlet?method=list";
        } catch (Exception e) {
            e.printStackTrace();
        }


        request.getRequestDispatcher(this.uri).forward((ServletRequest) request, (ServletResponse) response);
    }


    public void search(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            String currPage = request.getParameter("currentPage");

            String CommodityTitle = request.getParameter("title");

            if (currPage == null || "".equals(currPage.trim())) {
                currPage = "1";
            }

            int currentPage = Integer.parseInt(currPage);


            PageBean pageBean = new PageBean();
            pageBean.setCurrentPage(currentPage);

            Commodity title = new Commodity();
            title.setTitle(CommodityTitle);

            CommodityDAO dao = new CommodityDAO();
            Map<String, Object> result = dao.search(pageBean, title);

            request.setAttribute("result", result);

            this.uri = "/sys/goods/goods.jsp";
        } catch (Exception e) {
            e.printStackTrace();
        }


        request.getRequestDispatcher(this.uri).forward((ServletRequest) request, (ServletResponse) response);
    }


    public void Jumpup(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            int CommodityId = Integer.parseInt(request.getParameter("id"));

            Commodity cc = new Commodity();
            cc.setId(CommodityId);


            CommodityDAO dao = new CommodityDAO();


            Commodity result = dao.getone(cc);


            request.setAttribute("listClass", result);

            this.uri = "/sys/goods/update.jsp";
        } catch (Exception e) {
            e.printStackTrace();
        }


        request.getRequestDispatcher(this.uri).forward((ServletRequest) request, (ServletResponse) response);
    }

    public void init() throws ServletException {
    }
}
